<?php

include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/commands/Command.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/requestprocessors/PersonSectorInvolvementRequestProcessor.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/ErrorMessager.php';
/**
 * Description of InsertPersonSectorInvolvementCommand
 *
 * @author Reinowin7
 */
class InsertPersonSectorInvolvementCommand implements Command
{
    const OPERATION_SUCCESSFUL = 1;
    const OPERATION_UNSUCCESSFUL = 0;
    private $operationResult = 0;
    
    public function executeCommand($request)
    {
        $personSectorInvolvementRequestProcessor = null;
        $personSectorInvolvementFormConverter = null;
        $personSectorInvolvementFormData = null;
        $personSectorService = null;

        $personSectorInvolvementRequestProcessor = new PersonSectorInvolvementRequestProcessor();
        $personSectorInvolvementFormConverter = $personSectorInvolvementRequestProcessor->fillPersonSectorInvolvementFormConverterWithRequestData($_REQUEST);
        $personSectorInvolvementFormData = $personSectorInvolvementFormConverter->convertToModelData($_REQUEST);
        
        $personID = $personSectorInvolvementFormConverter->getPersonID();
        //echo $personID;
        $dataIsOKAfterFormatting = $personSectorInvolvementFormConverter->getIsDataAfterFormattingOK();
        
        //$_SESSION["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
        //$_REQUEST["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
        
        
        // first lets check if information has not been entered into a database before. If 
        // personID inside a personSectorInvolvementFormConverter is empty, then there have been no insertions before
        if ($dataIsOKAfterFormatting and (is_null($personID) or $personID == ""))
        {
            $personSectorService = new PersonSectorInvolvementServiceImpl();
            $wasInsertionToDatabaseSuccessful = $personSectorService->insertPersonWithSectorInvolvements($personSectorInvolvementFormData);

            if ($wasInsertionToDatabaseSuccessful == self::OPERATION_SUCCESSFUL)
            {
                // Person object inside a personSectorInvolvementFormConverter now has an id, which has to be put onto a session
                // so that we know that person and his/her sector involvement has been inserted once
                $personIDAfterInsertion = $personSectorInvolvementFormData->getPerson()->getPersonID();
                $personSectorInvolvementFormConverter->setPersonID($personIDAfterInsertion);
                $_SESSION["personID"] = $personIDAfterInsertion;
                $_REQUEST["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
                $this->operationResult = self::OPERATION_SUCCESSFUL;
            }
            else
            {
                $_REQUEST["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
                $_REQUEST["insertingPersonWithSectorInvolvementDataIntoADatabaseWasUnsuccessful"] = ErrorMessager::$E0001_ERROR_WHILE_TRYING_TO_INSERT_PERSON_WITH_SECTOR_INVOLVEMENT_DATA_INTO_DB;
                $this->operationResult = self::OPERATION_UNSUCCESSFUL;
            }
        }
        //update has to be done instead of an insertion
        elseif ($dataIsOKAfterFormatting and !is_null($personID) and $personID != "")
        {
            $personSectorService = new PersonSectorInvolvementServiceImpl();
            $wasUpdateInDatabaseSuccessful = $personSectorService->updatePersonWithSectorInvolvements($personSectorInvolvementFormData);

            if ($wasUpdateInDatabaseSuccessful == self::OPERATION_SUCCESSFUL)
            {
                $_REQUEST["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
                $this->operationResult = self::OPERATION_SUCCESSFUL;
            }
            else
            {
                $_REQUEST["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
                $_REQUEST["updatingPersonWithSectorInvolvementDataInADatabaseWasUnsuccessful"] = ErrorMessager::$E0002_ERROR_WHILE_TRYING_TO_UPDATE_PERSON_WITH_SECTOR_INVOLVEMENT_DATA_IN_DB;
                $this->operationResult = self::OPERATION_UNSUCCESSFUL;
            }
        }
        else
        {
            $_REQUEST["personSectorInvolvementFormConverter"] = serialize($personSectorInvolvementFormConverter);
            $this->operationResult = self::OPERATION_UNSUCCESSFUL;
        }

        return $this->operationResult;
    }
}

?>
